home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / OOP_Course / Examples / DrawTests / Composite / CompView.m < prev    next >
Text File  |  1992-12-19  |  3KB  |  124 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "CompView.h"
  5.  
  6. @implementation CompView
  7.  
  8. #define COPY 0
  9. #define SOVER 1
  10.  
  11. - initFrame:(const NXRect *)theFrame
  12. {
  13.     [super initFrame:theFrame];
  14.     currentPoint.x=oldPoint.x=bounds.size.width/2.0;
  15.     currentPoint.y=oldPoint.y=bounds.size.height/2.0;
  16.     sqSide=100.;
  17.     compType=NX_COPY;
  18.     [self setSqImage];            //create image of square
  19.     bgImage=[[NXImage alloc] initSize:&bounds.size];
  20.     backgroundGray=NX_WHITE;
  21.     [self setBgImage];            //create background image
  22.     return self;
  23. }
  24.  
  25. - (float)backgroundGray
  26. {
  27.     return backgroundGray;
  28. }
  29.  
  30. - takeBackgroundGrayFrom:sender
  31. {
  32.     backgroundGray = [sender floatValue];
  33.     [self setBgImage];            //reset background image with new gray
  34.     [self display];
  35.     return self;
  36. }
  37.  
  38. - compositeType:sender
  39. {
  40.     int comp;
  41.     comp=[[sender selectedCell] tag];    // read the composite type selected
  42.     if (comp == COPY) compType=NX_COPY;
  43.     if (comp == SOVER) compType=NX_SOVER;
  44.     return self;
  45. }
  46.  
  47. - setSqImage
  48. {   // This creates the squarewave image that is moved around
  49.     int    i, length = sqSide/8;
  50.     
  51.     NXSetRect(&sqRect,0.0,0.0,3*sqSide,sqSide);        //init the bounding area
  52.     sqImage=[[NXImage alloc] initSize:&sqRect.size];
  53.     [sqImage lockFocus];
  54.         [sqImage composite:NX_CLEAR fromRect:&sqRect toPoint:&sqRect.origin];
  55.         PSsetgray(NX_BLACK);
  56.         PSsetlinewidth(5.0);
  57.  
  58.         PSmoveto(sqRect.origin.x+2.25, sqRect.origin.y+2.25);
  59.         for(i = 0; i<12; i++){
  60.             PSrlineto(length, 0.0);
  61.             PSrlineto(0.0, 8*length);
  62.             PSrlineto(length, 0.0);
  63.             PSrlineto(0.0, -8*length);
  64.         }
  65.         PSstroke();
  66.     [sqImage unlockFocus];
  67.  
  68. /* Now set up square to move FROM in background */
  69.     oldRect.size=sqRect.size;        //The old square is always this size
  70.     oldRect.origin.x=currentPoint.x;    //These are used only at initialization 
  71.     oldRect.origin.y=currentPoint.y;
  72.     return self;
  73. }
  74.  
  75. - setBgImage
  76. {   //This creates the background image
  77.     [bgImage lockFocus];
  78.         PSsetgray(backgroundGray);
  79.         NXRectFill(&bounds);
  80.     [bgImage unlockFocus];
  81.     return self;
  82. }
  83.  
  84. - drawSelf:(const NXRect *)r :(int)c
  85. {   //Here we just display the entire background, then the squarewave
  86.     [bgImage composite:NX_COPY toPoint:&bounds.origin];
  87.     [sqImage composite:compType toPoint:¤tPoint];
  88.     return self;
  89. }
  90.  
  91. - moveSquare
  92. {
  93.    [self lockFocus];
  94.         /* First, erase by copying background rectangle on top of it */
  95.         [bgImage composite:NX_COPY fromRect:&oldRect toPoint:&oldRect.origin];
  96.         /* Now composite squarewave to new location */
  97.         [sqImage composite:compType toPoint:¤tPoint];
  98.     [self unlockFocus];
  99.     [window flushWindow];            // flush the window buffer to screen
  100.  
  101.     return self;
  102. }
  103.     
  104. - mouseDownAction:(NXPoint *)currentLocation
  105. {    
  106.     [self mouseDraggedAction:currentLocation];  //now do same as when dragging
  107.     return self;
  108. }
  109.  
  110. - mouseDraggedAction:(NXPoint *)currentLocation
  111. {   // Move square to follow the mouse
  112.     oldRect.origin=currentPoint;
  113.     xOffset = sqRect.size.width/2;    // set the offsets for movement wrt the center
  114.     yOffset = sqRect.size.height/2;
  115.     currentPoint.x  = currentLocation->x - xOffset; // new x and y locations are center
  116.     currentPoint.y  = currentLocation->y - yOffset; // of drawing
  117.  
  118.     [self moveSquare];
  119.     return self;
  120. }
  121.  
  122.  
  123. @end
  124.